import { rotation_delta } from "..";
import { IImageConstructor } from "../interfaces/IImageConstructor";
import { Bullet } from "./Bullet";

export class Player extends Phaser.Physics.Arcade.Sprite {

    private health: number;
    private lastShoot: number;
    private speed: number;

    private barrel: Phaser.GameObjects.Image;
    private lifeBar: Phaser.GameObjects.Graphics;

    private bullets: Phaser.GameObjects.Group;

    // input
    // private cursors: Phaser.Types.Input.Keyboard.CursorKeys;
    /* private rotateKeyLeft: Phaser.Input.Keyboard.Key;
    private rotateKeyRight: Phaser.Input.Keyboard.Key;
    private shootingKey: Phaser.Input.Keyboard.Key; */
    
    private cursorA: Phaser.Input.Keyboard.Key;
    private cursorS: Phaser.Input.Keyboard.Key;
    private cursorD: Phaser.Input.Keyboard.Key;
    private cursorW: Phaser.Input.Keyboard.Key;

    public getBullets(): Phaser.GameObjects.Group { return this.bullets; }

    constructor(param: IImageConstructor) {
        super(param.scene, param.x, param.y, param.texture);

        this.initImage();
        this.scene.add.existing(this);
        this.scene.physics.world.enable(this);
    }
    

    private initImage() {
        this.health = 1;
        this.lastShoot = 0;
        this.speed = 150;

        this.setOrigin(0.5, 0.5)
            ;
        this.angle = 180;
        
        this.barrel = this.scene.add.image(this.x, this.y, 'barrelBlue')
            .setOrigin(0.5, 1)
            .setDepth(1).setAngle(180)
            .setY(this.y + 80)
            ;

        this.lifeBar = this.scene.add.graphics();
        this.redrawLifeBar();


        this.bullets = this.scene.add.group({
            active: true,
            maxSize: 10,
            runChildUpdate: true
        });


        // input
        // this.cursors = this.scene.input.keyboard.createCursorKeys();
        /* this.rotateKeyLeft = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);
        this.rotateKeyRight = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
        this.shootingKey = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE); */
        
        this.cursorA = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);
        this.cursorS = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
        this.cursorD = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
        this.cursorW = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
        
        this.handleInput();
        this.handleShooting();
    }

    
    update(...args: any[]): void {
        if(this.active){
            this.barrel.x = this.x;
            this.barrel.y = this.y;

            this.lifeBar.x = this.x;
            this.lifeBar.y = this.y;
            
            this.handleMove();
        } else {
            this.destroy();
            this.barrel.destroy();
            this.lifeBar.destroy();
        }
    }
    
    private handleMove() {
        if(this.cursorS.isDown){
            this.scene.physics.velocityFromRotation(
                this.rotation - Math.PI/2, 
                this.speed, 
                this.body.velocity
            );
        } 
        if(this.cursorW.isDown){
            this.scene.physics.velocityFromRotation(
                this.rotation - Math.PI/2, 
                -this.speed, 
                this.body.velocity
            );
        } 
        if(this.cursorA.isDown){
            this.scene.physics.velocityFromRotation(
                this.rotation - Math.PI, 
                -this.speed, 
                this.body.velocity
            );
        }
        if(this.cursorD.isDown){
            this.scene.physics.velocityFromRotation(
                this.rotation + Math.PI,
                this.speed, 
                this.body.velocity
            );
        }
        
        if(!(this.cursorA.isDown || this.cursorS.isDown || this.cursorW.isDown || this.cursorD.isDown)) {
            this.setVelocity(0);
        }
    }
    
    private handleInput() {
        /* this.scene.input.keyboard.on('keydown-S', () => {
            this.scene.physics.velocityFromRotation(
                this.rotation - Math.PI/2, 
                this.speed, 
                this.body.velocity
            );
        });
        this.scene.input.keyboard.on('keyup-S', () => {
            this.setVelocity(0);
        });
        
        
        this.scene.input.keyboard.on('keydown-W', () => {
            this.scene.physics.velocityFromRotation(
                this.rotation - Math.PI/2, 
                -this.speed, 
                this.body.velocity
            );
        });
        this.scene.input.keyboard.on('keyup-W', () => {
            this.setVelocity(0);
        });
        
        
        this.scene.input.keyboard.on('keydown-A', () => {
            this.scene.physics.velocityFromRotation(
                this.rotation - Math.PI, 
                -this.speed, 
                this.body.velocity
            );
        })
        this.scene.input.keyboard.on('keyup-A', () => {
            this.setVelocity(0);
        });


        this.scene.input.keyboard.on('keydown-D', () => {
            this.scene.physics.velocityFromRotation(
                this.rotation + Math.PI,
                this.speed, 
                this.body.velocity
            );
        });
        this.scene.input.keyboard.on('keyup-D', () => {
            this.setVelocity(0);
        }); */
        
        
        this.scene.input.keyboard.on('keydown-LEFT', () => {
            if(this.barrel.rotation > -Math.PI/2 && this.barrel.rotation < Math.PI/4){
                this.barrel.rotation -= Math.PI/24;
                if(this.barrel.rotation < -Math.PI/2){
                    this.barrel.rotation = -Math.PI/2;
                }
            }

            if(this.barrel.rotation < -Math.PI/2 && this.barrel.rotation >= -Math.PI){
                this.barrel.rotation += Math.PI/24;
            }

            if(this.barrel.rotation <= Math.PI && this.barrel.rotation > 3/4*Math.PI){
                this.barrel.rotation += Math.PI/24;
            }
        });

        this.scene.input.keyboard.on('keyup-UP', () => {
            /* if( Math.abs(Math.abs(this.barrel.rotation) - Math.PI) < Math.PI/24  ){
                return;
            }

            if( Math.abs(Math.abs(this.barrel.rotation) - 0) < Math.PI/24 ){
                return;
            } */
            
            if( this.barrel.rotation >= -Math.PI && this.barrel.rotation <= 0){
                this.barrel.rotation += Math.PI/24;
            }

            if( this.barrel.rotation <= Math.PI && this.barrel.rotation >= 0){
                this.barrel.rotation -= Math.PI/24;
            }
        });


        this.scene.input.keyboard.on('keyup-DOWN', () => {
            /* if( Math.abs(Math.abs(this.barrel.rotation) - Math.PI) < Math.PI/24  ){
                return;
            }

            if( Math.abs(Math.abs(this.barrel.rotation) - 0) < Math.PI/24 ){
                return;
            } */
            
            if( this.barrel.rotation >= -Math.PI && this.barrel.rotation <= 0){
                this.barrel.rotation -= Math.PI/24;
                if(this.barrel.rotation > 0){
                    this.barrel.rotation = -Math.PI;
                }
                return;
            }

            if( this.barrel.rotation <= Math.PI && this.barrel.rotation >= 0){
               this.barrel.rotation += Math.PI/24;
            }
        })

        this.scene.input.keyboard.on('keydown-RIGHT', () => {
            if(this.barrel.rotation > -Math.PI/4 && this.barrel.rotation < Math.PI/2){
                this.barrel.rotation += Math.PI/24;
            }

            if(this.barrel.rotation < -3/4*Math.PI && this.barrel.rotation >= -Math.PI){
                this.barrel.rotation -= Math.PI/24;
            }

            if(this.barrel.rotation <= Math.PI && this.barrel.rotation > Math.PI/2){
                this.barrel.rotation -= Math.PI/24;
            }
        });
        
        this.scene.input.keyboard.on('keydown-SPACE', () => {
            this.handleShooting();
        });
    }
    

    private handleShooting() {
        // if(this.shootingKey.isDown && this.scene.time.now > this.lastShoot){
            /* this.scene.cameras.main.shake(5, 0.05);
            this.scene.tweens.add({
                targets: this,
                props: { alpha: 0.5 },
                delay: 0,
                duration: 5,
                ease: 'Power1',
                easeParams: null,
                hold: 0,
                repeat: 0,
                repeatDelay: 0,
                yoyo: true,
                paused: false
            }); */

            
            if(this.bullets.getLength() < 15){
                this.bullets.add(
                    new Bullet({
                        scene: this.scene,
                        x: this.barrel.x,
                        y: this.barrel.y,
                        rotation: this.barrel.rotation,
                        texture: 'bulletBlue'
                    })
                );
            }
        // }
    }
    
    private redrawLifeBar() {
        this.lifeBar.clear();
        this.lifeBar.fillStyle(0xe66a28, 1)
            .fillRect(
                -this.width/2,
                this.height/2,
                this.width * this.health,
                15
            )
            .lineStyle(2, 0xffffff)
            .strokeRect(
                -this.width/2,
                this.height/2,
                this.width,
                15
            )
            .setDepth(1)
            ;
    }

    public updateHealth() {
        if(this.health > 0){
            this.health -= 0.05;
            this.redrawLifeBar();
        } else {
            this.health = 0;
            this.active = false;
            this.scene.scene.start('MenuScene');
        }
    }
}
